package com.hhss.qishi.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hhss.qishi.config.pagehelper.PageVO;
import com.hhss.qishi.entity.Article;
import com.hhss.qishi.service.ArticleService;
import com.hhss.qishi.utils.ConvertPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author :Administrator
 * @Description
 * @Date :2019-05-27 15:28.
 */
@RestController
@RequestMapping("articles")
public class ArticleController {

	@Autowired
	private ArticleService articleService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public Map<String, Object> getList(Article article){
		Page<Article> page= PageHelper.startPage(article.getPage(), article.getLimit());
		articleService.getList(article);
		PageVO<Article> vo = PageVO.build(page);
		return ConvertPageUtil.Convert(vo);
	}

	@RequestMapping(value = "/", method = RequestMethod.POST)
	public int add(Article article){
		return articleService.add(article);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public Article getById(@PathVariable Long id) {
		return articleService.getById(id);
	}

	@RequestMapping(value = "/", method = RequestMethod.PUT)
	public int update(Article article) {
		return articleService.updateById(article);
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public int deleteById(@PathVariable Long id) {
		return articleService.deleteById(id);
	}
}
